import request from './http.js'
/**
 * 微信登录
 * @param {string} code - 微信登录 code
 */
export function wechatLogin(code) {
  return request({
    url:'/user/wechatlogin',
    method:'POST',
    data: {code}
  })  
}

/**
 * 解密手机号
 * @param {object} data - 包含加密数据的对象
 */
export function decryptPhoneNumber(data) {
  return request({
      url: '/user/decryptPhoneNumber',
      method: 'POST',
      data
  })
}

/**
 * 根据 ID 获取食物详情
 * @param {number} id - 食物 ID
 */
export function getFoodById(id) {
  return request({
    url: `/food/${id}`, // 使用反引号和模板字符串
    method: 'GET'
  });
}

/**
 * 根据分类获取食物
 * @param {object} data - 查询条件
 */
export function getFoodByCategory(data) {
  return request({
    url:'/food',
    method:'GET',
    params:data
  })
}

/**
 * 模糊查询食物和分类
 * @param {object} data - 关键词
 */
export function getFoodsByKeyword(keyword) {
  return request({
    url:'/food/search',
    method: 'GET',
    params:{keyword}
  })
}
/**
 * 添加到dietrecord
 * @param {object} dietRecord 
 * 
 */
export const addDietRecord = (dietRecord) => {
  return request({
    url: '/dietRecord',
    method: 'POST',
    data: dietRecord
  });
};

/**
 * 添加到foodentry
 * @param {object} foodentry 
 * 
 */
export const addFoodEntry = (foodentry) => {
  return request({
    url: '/foodEntries',
    method: 'POST',
    data: foodentry
  })
}

export const addPlanFood = (userId, foodId, mealType, grams) => {
  return request({
    url:'/dietPlans/addFoodToPlan',
    method: 'POST',
    data: { userId, foodId, mealType, grams }
  })
}

/**
 * 饮食记录相关接口
 * @param {*} userId 
 * @param {*} mealType 
 */
export const getDietRecordsByUserIdAndMealType = (userId, mealType) => {
  return request({
    url: `/dietRecord/${userId}/meals/${mealType}`,
    method: 'GET'
  });
};

export const getAllDietRecordsByUserId = (userId) => {
  return request({
    url: `/dietRecord/user/${userId}`,
    method: 'GET'
  });
};

export const updateNutrition = (userId,date) => {
  return request({
    url:`/dietRecord/nutrition/summary/${userId}`,
    method: 'GET',
    params: {
      date: date // 添加日期参数
    }
  })
}
export const updatePeriodNutrition = (userId, period, date) => {
  return request({
    url: `/dietRecord/nutritionPeriod/summary/${userId}`,
    method: 'GET',
    params: {
      period: period, // 按天查询
      date: date // 具体日期
    },    
  })
}

export const getPlanSummary = (userId) => {
  return request({
    url:`/dietPlans/summary/${userId}`,
    method: 'GET',
  })
}

export const getCoachRecommendations = () => {
  return request({
    url:'/coachRecommendations',
    method: 'GET'
  })
}

export const applyCoachRecommendation  = (userId,recommendationId) => {
  return request({
    url: `/dietPlans/apply-recommendation/${userId}/${recommendationId}`,
    method: 'POST'
  })
}

export const deleteRecordAndFoodEntry = (foodEntryId) => {
  return request({
    url: `/dietRecord/foodEntry/${foodEntryId}`,
    method: 'DELETE'
  });
}

export const deletePlanFoodById = (id) => {
  return request({
    url: `/dietPlans/delete/${id}`,
    method: 'DELETE'
  });
};

export const loadNewsDetail = (newsId,userId) => {
  return request({
    url: '/news/detail',
    method: 'GET',
    params: {
      id: newsId,
      userId
    }
  })
}

export const userlike = (isLike, newsId, userId) => {
  return request({
    url: isLike ? '/userLike/cancel' : '/userLike/add',
    method: 'POST',
    data: {
      userId,
      newsId
    }
  })
}

export const getNewsList = (page, pageSize) => {
  return request({
    url: '/news/list',
    method: 'GET',
    params: {
      page,
      pageSize
    }
  });
};